<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>charlistdecompressor.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_i18n_charlistdecompressor.js.html">charlistdecompressor.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2009 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview The decompressor for Base88 compressed character lists.
<a name="line17"></a> *
<a name="line18"></a> * The compression is by base 88 encoding the delta between two adjacent
<a name="line19"></a> * characters in ths list. The deltas can be positive or negative. Also, there
<a name="line20"></a> * would be character ranges. These three types of values
<a name="line21"></a> * are given enum values 0, 1 and 2 respectively. Initial 3 bits are used for
<a name="line22"></a> * encoding the type and total length of the encoded value. Length enums 0, 1
<a name="line23"></a> * and 2 represents lengths 1, 2 and 4. So (value * 8 + type * 3 + length enum)
<a name="line24"></a> * is encoded in base 88 by following characters for numbers from 0 to 87:
<a name="line25"></a> * 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ (continued in next line)
<a name="line26"></a> * abcdefghijklmnopqrstuvwxyz!#$%()*+,-.:;&lt;=&gt;?@[]^_`{|}~
<a name="line27"></a> *
<a name="line28"></a> * Value uses 0 based counting. That is value for the range [a, b] is 0 and
<a name="line29"></a> * that of [a, c] is 1. Simillarly, the delta of &quot;ab&quot; is 0.
<a name="line30"></a> *
<a name="line31"></a> * Following python script can be used to compress character lists taken
<a name="line32"></a> * standard input: http://go/charlistcompressor.py
<a name="line33"></a> *
<a name="line34"></a> */
<a name="line35"></a>
<a name="line36"></a>goog.provide(&#39;goog.i18n.CharListDecompressor&#39;);
<a name="line37"></a>
<a name="line38"></a>goog.require(&#39;goog.array&#39;);
<a name="line39"></a>goog.require(&#39;goog.i18n.uChar&#39;);
<a name="line40"></a>
<a name="line41"></a>
<a name="line42"></a>
<a name="line43"></a>/**
<a name="line44"></a> * Class to decompress base88 compressed character list.
<a name="line45"></a> * @constructor
<a name="line46"></a> */
<a name="line47"></a>goog.i18n.CharListDecompressor = function() {
<a name="line48"></a>  this.buildCharMap_(&#39;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr&#39; +
<a name="line49"></a>      &#39;stuvwxyz!#$%()*+,-.:;&lt;=&gt;?@[]^_`{|}~&#39;);
<a name="line50"></a>};
<a name="line51"></a>
<a name="line52"></a>
<a name="line53"></a>/**
<a name="line54"></a> * 1-1 mapping from ascii characters used in encoding to an integer in the
<a name="line55"></a> * range 0 to 87.
<a name="line56"></a> * @type {Object}
<a name="line57"></a> * @private
<a name="line58"></a> */
<a name="line59"></a>goog.i18n.CharListDecompressor.prototype.charMap_ = null;
<a name="line60"></a>
<a name="line61"></a>
<a name="line62"></a>/**
<a name="line63"></a> * Builds the map from ascii characters used for the base88 scheme to number
<a name="line64"></a> * each character represents.
<a name="line65"></a> * @param {string} str The string of characters used in base88 scheme.
<a name="line66"></a> * @private
<a name="line67"></a> */
<a name="line68"></a>goog.i18n.CharListDecompressor.prototype.buildCharMap_ = function(str) {
<a name="line69"></a>  if (!this.charMap_) {
<a name="line70"></a>    this.charMap_ = {};
<a name="line71"></a>    for (var i = 0; i &lt; str.length; i++) {
<a name="line72"></a>      this.charMap_[str.charAt(i)] = i;
<a name="line73"></a>    }
<a name="line74"></a>  }
<a name="line75"></a>};
<a name="line76"></a>
<a name="line77"></a>
<a name="line78"></a>/**
<a name="line79"></a> * Gets the number encoded in base88 scheme by a substring of given length
<a name="line80"></a> * and placed at the a given position of the string.
<a name="line81"></a> * @param {string} str String containing sequence of characters encoding a
<a name="line82"></a> *     number in base 88 scheme.
<a name="line83"></a> * @param {number} start Starting position of substring encoding the number.
<a name="line84"></a> * @param {number} leng Length of the substring encoding the number.
<a name="line85"></a> * @return {number} The encoded number.
<a name="line86"></a> * @private
<a name="line87"></a> */
<a name="line88"></a>goog.i18n.CharListDecompressor.prototype.getCodeAt_ = function(str, start,
<a name="line89"></a>    leng) {
<a name="line90"></a>  var result = 0;
<a name="line91"></a>  for (var i = 0; i &lt; leng; i++) {
<a name="line92"></a>    var c = this.charMap_[str.charAt(start + i)];
<a name="line93"></a>    result += c * Math.pow(88, i);
<a name="line94"></a>  }
<a name="line95"></a>  return result;
<a name="line96"></a>};
<a name="line97"></a>
<a name="line98"></a>
<a name="line99"></a>/**
<a name="line100"></a> * Add character(s) specified by the value and type to given list and return
<a name="line101"></a> * the next character in the sequence.
<a name="line102"></a> * @param {Array.&lt;string&gt;} list The list of characters to which the specified
<a name="line103"></a> *     characters are appended.
<a name="line104"></a> * @param {number} lastcode The last codepoint that was added to the list.
<a name="line105"></a> * @param {number} value The value component that representing the delta or
<a name="line106"></a> *      range.
<a name="line107"></a> * @param {number} type The type component that representing whether the value
<a name="line108"></a> *      is a positive or negative delta or range.
<a name="line109"></a> * @return {number} Last codepoint that is added to the list.
<a name="line110"></a> * @private
<a name="line111"></a> */
<a name="line112"></a>goog.i18n.CharListDecompressor.prototype.addChars_ = function(list, lastcode,
<a name="line113"></a>    value, type) {
<a name="line114"></a>   if (type == 0) {
<a name="line115"></a>     lastcode += value + 1;
<a name="line116"></a>     goog.array.extend(list, goog.i18n.uChar.fromCharCode(lastcode));
<a name="line117"></a>   } else if (type == 1) {
<a name="line118"></a>     lastcode -= value + 1;
<a name="line119"></a>     goog.array.extend(list, goog.i18n.uChar.fromCharCode(lastcode));
<a name="line120"></a>   } else if (type == 2) {
<a name="line121"></a>     for (var i = 0; i &lt;= value; i++) {
<a name="line122"></a>       lastcode++;
<a name="line123"></a>       goog.array.extend(list, goog.i18n.uChar.fromCharCode(lastcode));
<a name="line124"></a>     }
<a name="line125"></a>   }
<a name="line126"></a>  return lastcode;
<a name="line127"></a>};
<a name="line128"></a>
<a name="line129"></a>
<a name="line130"></a>/**
<a name="line131"></a> * Gets the list of characters specified in the given string by base 88 scheme.
<a name="line132"></a> * @param {string} str The string encoding character list.
<a name="line133"></a> * @return {Array.&lt;string&gt;} The list of characters specified by the given string
<a name="line134"></a> *     in base 88 scheme.
<a name="line135"></a> */
<a name="line136"></a>goog.i18n.CharListDecompressor.prototype.toCharList = function(str) {
<a name="line137"></a>  var metasize = 8;
<a name="line138"></a>  var result = [];
<a name="line139"></a>  var lastcode = 0;
<a name="line140"></a>  var i = 0;
<a name="line141"></a>  while (i &lt; str.length) {
<a name="line142"></a>    var c = this.charMap_[str.charAt(i)];
<a name="line143"></a>    var meta = c % metasize;
<a name="line144"></a>    var type = Math.floor(meta / 3);
<a name="line145"></a>    var leng = (meta % 3) + 1;
<a name="line146"></a>    if (leng == 3) {
<a name="line147"></a>      leng++;
<a name="line148"></a>    }
<a name="line149"></a>    var code = this.getCodeAt_(str, i, leng);
<a name="line150"></a>    var value = Math.floor(code / metasize);
<a name="line151"></a>    lastcode = this.addChars_(result, lastcode, value, type);
<a name="line152"></a>
<a name="line153"></a>    i += leng;
<a name="line154"></a>  }
<a name="line155"></a>  return result;
<a name="line156"></a>};
</pre>


</body>
</html>
